import Vue from 'vue'
import VueRouter from 'vue-router'
import publicRoute from './modules/public'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {
  getRouter
} from '@/utils/index'

// import cashier from './cashier'
// import principal from './principal'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: getRouter('public', 'Login')
  },
  {
    path: '*',
    name: '404',
    component: getRouter('error', '404')
  },
  ...publicRoute
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/**
 * 添加页面加载进度条nProgress
 */
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (to.path === '/') {
    next({
      path: '/login'
    })
  } else {
    next()
  }
})

router.onError(() => {
  NProgress.done() // 结束Progress
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})

export default router
